/*
 * Bitwise operations. This program works on a set of 8 boxes. Each box can
 * either be "empty" or "filled". The program uses one variable for all the 8
 * boxes.
*/

#include <iostream>
using namespace std;

void printBinary(const unsigned char val) {
  for(int i = 7; i >= 0; i--)
    if(val & (1 << i)) 
      std::cout << "1";
    else
      std::cout << "0";
    cout << endl;
}

// Sets all bits of BOXES to 0 (all boxes become "empty")
void Box_Init(unsigned char *boxes)
{
    *boxes = *boxes & 0;
}

// Sets box BOXNUM to be "filled"
void Box_Set_Filled(unsigned char *boxes, int boxnum)
{
    if ( boxnum < 0 || boxnum > 7 ) {
        cerr << "Box no. out of range (0-7)" << endl;
    }
    *boxes = *boxes | (1 << boxnum);
}

// Sets box BOXNUM to be "empty"
void Box_Set_Empty(unsigned char *boxes, int boxnum)
{
    if ( boxnum < 0 || boxnum > 7 ) {
        cerr << "Box no. out of range (0-7)" << endl;
    }
    *boxes = *boxes & ~(1 << boxnum);
}

int
main(int argc, char *argv[])
{
    unsigned char boxes;

    Box_Init(&boxes);
    printBinary(boxes);
    Box_Set_Filled(&boxes, 2);
    printBinary(boxes);
    Box_Set_Filled(&boxes, 7);
    printBinary(boxes);
    Box_Set_Empty(&boxes, 7);
    printBinary(boxes);

    return 0;
}

